<?php
namespace app\common\controller;

use com\Auth;
use think\Loader;
use think\Controller;
use think\Db;

/**
* 后台公用基础控制器
* @author Kaisa
* @version 2017-02-09
*/
class AdminBaseController extends Controller {
	
    protected function _initialize() {
        parent::_initialize();
        $this->checkAuth();
        $this->getMenu();
		$site_config = get_config('site_config');//配置
		config($site_config);//配置
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth() {
		$admin_id = session('admin_id');
        if (!$admin_id) {
            $this->redirect('admin/login/index');
        }

		$url = request()->module() . '/' . request()->controller() . '/' . request()->action();
        // 排除权限
        $not_check = ['admin/Index/index', 'admin/Index/indexpage'];
		// 排除管理员ID
		$not_check_id = [1];
		if(!in_array($admin_id,$not_check_id) && !in_array($url,$not_check)){ 
          	  $auth = new Auth();
            		if (!$auth->check($url, $admin_id)) {
             		   $this->error('没有权限');
            		}
    }


}










    /**
     * 获取侧边栏菜单
     */
    protected function getMenu() {
        $menu     = [];
        $menu1     = [];
        $admin_id = session('admin_id');
        $id=Db::name('auth_group_access')->where('uid',$admin_id)->field('group_id')->find();
		$role = Db::name('auth_group')->field('rules')->where('id',$id['group_id'])->find();
		$array=explode(',',$role['rules']);
        $auth_rule_list = Db::name('auth_rule')->order(['sort' => 'asc', 'id' => 'asc'])->where('status',1)->select();
        foreach ($auth_rule_list as $value) {
        	if($id['group_id']!=1){
         		  if (in_array($value['id'], $array)) {
                		$menu[] = $value;
           	 		}
         }else{
         	$menu[] = $value;
       	  }
        }
        
        $menu = !empty($menu) ? array2tree($menu) : [];
        $this->assign('menu', $menu);
    }
}
